package com.sec.etech.form.cust.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.sec.etech.form.cust.service.EtechFormCustSqlViewManager;
import org.openbpm.base.api.aop.annotion.CatchErr;
import org.openbpm.base.api.response.impl.ResultMsg;
import org.openbpm.base.core.util.StringUtil;
import org.openbpm.form.manager.FormCustSqlViewManager;
import org.openbpm.form.model.custsql.FormCustSql;
import org.openbpm.form.request.IdRequest;
import org.openbpm.form.request.SqlViewRequest;
import org.openbpm.form.rest.controller.FormCustSqlViewController;
import org.openbpm.form.vo.SqlButtonVO;
import org.openbpm.form.vo.SqlViewDataVO;
import org.openbpm.form.vo.SqlViewVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

@RestController
@RequestMapping({"/etech/formCustSql/view"})
public class EtechFormCustSqlViewController extends FormCustSqlViewController {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Resource(name="etechFormCustSqlViewManager")
    FormCustSqlViewManager formCustSqlViewManager;


    @RequestMapping({"/vo_{code}"})
    @CatchErr
    public ResultMsg<FormCustSql> getVo(@PathVariable("code") String code) {
        if(this.logger.isDebugEnabled()){
            this.logger.debug("根据别名获取自定义sql对象,code:{}", code);
        }
        return this.formCustSqlViewManager.getVo(code);
    }

    @RequestMapping({"/button_{code}"})
    @CatchErr
    public ResultMsg<List<JSONObject>> getVoButtons(@PathVariable("code") String code) {
        if(this.logger.isDebugEnabled()){
            this.logger.info("根据别名获取自定义sql对象的按钮列表,code:{}", code);
        }
        List<JSONObject> buttonList = new ArrayList<>();
        List<SqlButtonVO> buttonVOList =  ((EtechFormCustSqlViewManager)this.formCustSqlViewManager).getVoButtons(code).getData();
        String btnRightAlias = null;
        for(SqlButtonVO sqlButtonVO: buttonVOList){
            // 检查按钮权限
            btnRightAlias = null;
            if(sqlButtonVO.getJoinBtnRight()!=null && 1==sqlButtonVO.getJoinBtnRight()){
                btnRightAlias = "customsql_button_"+code+"_"+sqlButtonVO.getAlias();
            }
            JSONObject buttonJson = new JSONObject();
            BeanUtil.copyProperties(sqlButtonVO, buttonJson);
            if(btnRightAlias!=null){
                buttonJson.put("btnRightAlias", btnRightAlias);
            }
            buttonList.add(buttonJson);
        }


        return ResultMsg.SUCCESS(buttonList);
    }

    @RequestMapping({"/list_{code}"})
    @ResponseBody
    @CatchErr
    public ResultMsg<SqlViewDataVO> listData(@PathVariable("code") String code, @RequestParam(value = "bpmExt", required = false) String bpmExt, @RequestBody SqlViewRequest request) {
        if(this.logger.isDebugEnabled()){
            this.logger.info("自定义列表-数据查询,code:{}", code);
        }

        if (StringUtil.isEmpty(code)) {
            return ResultMsg.ERROR("编码不能为空");
        }

        request.setCode(code);

        if (StringUtil.isNotEmpty(bpmExt)) {
            request.setBpmExt(bpmExt);
        }


        return this.formCustSqlViewManager.listData(request);
    }


    @RequestMapping({"/sql_{code}"})
    @ResponseBody
    @CatchErr
    public ResultMsg<SqlViewVO> listDataSql(@PathVariable("code") String code, @RequestBody SqlViewRequest request) {
        if(this.logger.isDebugEnabled()){
            this.logger.info("自定义列表-数据查询,code:{}", code);
        }

        if (StringUtil.isEmpty(code)) {
            return ResultMsg.ERROR("编码不能为空");
        }

        request.setCode(code);

        return this.formCustSqlViewManager.listDataSql(request);
    }


    @RequestMapping({"/export2_{code}"})
    @ResponseBody
    @CatchErr
    public ResultMsg<String> export2(@PathVariable("code") String code, SqlViewRequest sqlViewRequest, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtil.isEmpty(code)) {
            this.logger.error("编码不能为空");
            return ResultMsg.ERROR("编码不能为空");
        }
        if(StringUtil.isEmpty(sqlViewRequest.getParams())){
            JSONObject jsonParams = JSONObject.parseObject(request.getParameter("param"));
            sqlViewRequest.setParams(jsonParams.getJSONObject("params").toJSONString());
        }
        sqlViewRequest.setCode(code);
        return this.formCustSqlViewManager.exportData(sqlViewRequest, response);
    }


    @RequestMapping({"/changeData_{code}_{buttonAlias}"})
    public ResultMsg<String> changeData(@PathVariable("code") String code, @PathVariable("buttonAlias") String buttonAlias, @RequestBody IdRequest request) {
        if (StringUtil.isEmpty(code)) {
            this.logger.error("编码不能为空");
            return ResultMsg.ERROR("编码不能为空");
        }

        if (StringUtil.isEmpty(buttonAlias)) {
            this.logger.error("按钮别名不能为空");
            return ResultMsg.ERROR("按钮别名不能为空");
        }

        String id = request.getId();
        if (StringUtil.isEmpty(id)) {
            this.logger.error("ID不能为空");
            return ResultMsg.ERROR("ID不能为空");
        }

        return this.formCustSqlViewManager.changeData(code, buttonAlias, id);
    }

}
